package com.greatwideweb.sandbox;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

public class AndroidAudioSandBoxActivity extends Activity implements OnClickListener, IMediaPlayerTask{
    /** Called when the activity is first created. */
	
	MediaPlayer mp=null;
	Button btnPlay=null;
	Button btnStop=null;
	boolean hasInit=false;
	boolean hasAudio =false;
	MediaPlayerTask mpt=null;
	MediaPlayer mediaPlayerRef=null;
	LinearLayout waitLayout=null;
	ProgressBar pb=null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnPlay = (Button) findViewById(R.id.btnPLay);
        btnPlay.setOnClickListener(this);
        
        btnStop = (Button) findViewById(R.id.btnStop);
        btnStop.setOnClickListener(this);
        
        waitLayout = (LinearLayout) findViewById(R.id.loMediaDetails);
        pb = (ProgressBar) findViewById(R.id.ProgressBar01);
        
    }
    
    
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.equals(btnPlay)) {
		  waitLayout.setVisibility(1);
		  mediaPlayerRef = null;
		  btnPlay.setEnabled(false);
			mpt = new MediaPlayerTask();
			mpt.registerObserver(this);
			mpt.execute("http://provisioning.streamtheworld.com/pls/WPENFMAAC.pls");
			
		}else {
			
			if(mediaPlayerRef != null){
				mediaPlayerRef.stop();	
			}else {
				Toast.makeText(this, "There is nothign to stop", Toast.LENGTH_SHORT).show();
			}
			btnStop.setEnabled(false);
			btnPlay.setEnabled(true);
			
		}
		
		
	}

  @Override
  public void onMediaPlayerComplete(MediaPlayer mp) {
    waitLayout.setVisibility(4);
    mediaPlayerRef = mp;
    mediaPlayerRef.start();
    btnPlay.setEnabled(false);
    btnStop.setEnabled(true);
  }
  
  public void onMediaPlayerError() {
    btnPlay.setEnabled(false);
    btnStop.setEnabled(true);
    Toast.makeText(this, "There was an error playing the audio feed", Toast.LENGTH_SHORT).show();
    
  }
}